package com.sunmi.sbs.commons.utils;

import java.util.*;

import java.io.File;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * author: fanjia
 * date: 2020/10/15 18:56
 * file: XMLUtils.java
 */
public class XMLUtils {


    public static Element getRoot(String filepath) throws DocumentException {
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File(filepath));
        Element root = doc.getRootElement();
        return root;
    }

    public static Element getFirstTreeNode(Element element) {
        Iterator<Element> it = element.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
//            System.out.println(e);
            if (("hashTree").equals(e.getName())) {
                return e;
            }
        }
        return null;
    }

    public static Element getRootHashTree(Element root) {
        Iterator<Element> it = root.elementIterator();
        Element hashTree = it.next();
        Element rootHashTree = getFirstTreeNode(hashTree);
        return rootHashTree;

    }

    public static Element getHashTreeNodeAfterElement(Element objectElement, String objectElementType, String testname) {
        boolean flag = false;
        Iterator<Element> it = objectElement.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
//            System.out.println(e);
            if ((objectElementType).equals(e.getName()) && (testname.equals(e.attributeValue("testname")))) {
                flag = true;
//                System.out.println("Found hashTree");
            }
            if (flag) {
                if (("hashTree").equals(e.getName())) {
//                    System.out.println(e);
                    return e;
                }
            }
        }
        return null;

    }

    public static Element getKidNodeByType(Element parentNode, String elementType) {
        Iterator<Element> it = parentNode.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
            if ((elementType).equals(e.getName())&& ("true".equals(e.attributeValue("enabled").toLowerCase()))) {
//                System.out.println("Found " + elementType);
                return e;
            }
        }
        return null;
    }

    public static List<Element> getKidNodesByType(Element parentNode, String elementType) {
        List<Element> list = new ArrayList<>();
        Iterator<Element> it = parentNode.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
            if ((elementType).equals(e.getName()) && ("true".equals(e.attributeValue("enabled").toLowerCase()))) {
//                System.out.println("Found " + elementType);
                list.add(e);
            }
        }
        return list;
    }

    public static Element getKidNodeByName(Element parentNode, String elementType, String testname) {
        Iterator<Element> it = parentNode.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
            if ((elementType).equals(e.getName()) && (e.attributeValue("testname").toLowerCase().contains(testname.toLowerCase())) && ("true".equals(e.attributeValue("enabled").toLowerCase()))) {
//                System.out.println("Found " + elementType);
                return e;
            }
        }
        return null;

    }

    public static List<Element> getKidNodesByName(Element parentNode, String elementType, String testname) {
        List<Element> list = new ArrayList<>();
        Iterator<Element> it = parentNode.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
            if ((elementType).equals(e.getName()) && (e.attributeValue("testname").toLowerCase().contains(testname.toLowerCase()))&& ("true".equals(e.attributeValue("enabled").toLowerCase()))) {
//                System.out.println("Found " + elementType);
                list.add(e);
            }
        }
        return list;

    }


    public static void findKidNodes(Element element) {
        Iterator<Element> it = element.elementIterator();
        while (it.hasNext()) {
            Element e = it.next();
            System.out.println(e);
            System.out.println(e.attributeValue("testname"));
        }
    }

    public static List<String> getElementsName(List<Element> stepList) {
        List<String> list = new ArrayList();
        for (Element step : stepList) {
            list.add(step.attributeValue("testname"));
        }
        return list;
    }

    public static String getElementName(Element element) {
        if (element != null) {
            return element.attributeValue("testname");

        } else return null;
    }


    public static LinkedHashMap<String, List> getCase(String filePath, String threadName, String caseName) throws DocumentException {
        LinkedHashMap<String, List> hashMap = new LinkedHashMap<>();

        Element root = getRoot(filePath);
        Element rootHashTree = getRootHashTree(root);
        Element threadHashTree = getHashTreeNodeAfterElement(rootHashTree, "ThreadGroup", threadName);
        Element caseHashTree = getHashTreeNodeAfterElement(threadHashTree, "GenericController", caseName);
        List<Element> caseList;
        caseList = getKidNodesByName(caseHashTree, "GenericController", "CASE");
        for (Element testcase : caseList) {
//            System.out.println(testcase);
            Element stepHashTree = getHashTreeNodeAfterElement(caseHashTree, "GenericController", testcase.attributeValue("testname"));
            List<Element> stepList;
            stepList = getKidNodesByName(stepHashTree, "GenericController", "步骤");
//            System.out.println("步骤数 " + stepList.size());

            for (Element step : stepList) {
//                System.out.println(step);
                Element beanShellHashTree = getHashTreeNodeAfterElement(stepHashTree, "GenericController", step.attributeValue("testname"));
//                System.out.println(beanShellHashTree);
                Element beanShellNode = getKidNodeByType(beanShellHashTree, "BeanShellSampler");
                System.out.println(beanShellNode);
                if (beanShellNode != null) {
                    System.out.println(beanShellNode.attributeValue("testname"));

                }
            }

            hashMap.put(testcase.attributeValue("testname"), getElementsName(stepList));
        }

        return hashMap;
    }


}
